
capture log close
log using $data/EDS_12_average_marginal_effects, text replace

clear
clear matrix
clear mata
set mem 2g
set matsize 2000
set more off
set maxvar 20000
set trace off
set varabbrev off




#d;
	  global cov " _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
	_Isu_ausbila1 _Isu_ausbila2
	_Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
	_Ichild_1 _Ichild_2
	_Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
	_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
	_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
	_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
	_Idauloint__13 _Idauloint__14
	female east_germany
    german migration_background 
    married  
	benefit_status benefit_level log_benefit_level
    lifetime_ue_age lifetime_empl_age last_wage last_wage_mis 
    region_ba_aloq_zivall gdp_real_pc region_ba_vacancies 
	sector_manufacturing sector_service  log_mobirate_avg5
	openness_st conscient_st extraversion_st neuroticism_st loc_st
	write_ger speak_ger write_eng speak_eng
	homeowner  
	   ";		

	     global cov2 "
	  _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
	_Isu_ausbila1 _Isu_ausbila2
	_Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
	_Ichild_1 _Ichild_2
	_Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
	_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
	_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
	_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
	_Idauloint__13 _Idauloint__14
	female east_germany
    german migration_background 
    married  
	benefit_status c.benefit_level c.log_benefit_level
    c.lifetime_ue_age c.lifetime_empl_age c.last_wage last_wage_mis 
    c.region_ba_aloq_zivall c.gdp_real_pc c.region_ba_vacancies 
	c.sector_manufacturing c.sector_service  c.log_mobirate_avg5
	c.openness_st c.conscient_st c.extraversion_st c.neuroticism_st c.loc_st
	write_ger speak_ger write_eng speak_eng
	homeowner  
	  ";

		global outc"
		    search_dist50 
	       log_distance_km 
		    applications_total
		   applications_distant
		   applications_local
		   channel_active
		   ";
	  #d cr




		use $data/data_pair, clear
		
		
		
		
	gen intensity_high=0
	
	forvalues x=1/487 {
		qui sum log_intensity_alt if pair_`x'==1
		qui replace intensity_high=1 if log_intensity_alt>r(mean) & pair_`x'==1 
	}
		
		
  
		encode region_pair, g(region_pair2)
		
		qui reg log_intensity_alt $cov i.region_pair2 [pw=1/fweight_reg], cluster(region)
		predict int_res, xb
		sum int_res, d
		
		sum int_res if intensity_high==1
		gen max_high = r(max)
		gen min_high = r(min)
		
		sum int_res if intensity_high==0
		gen max_low = r(max)
		gen min_low = r(min)
		
		egen pmax = rmin(max_high max_low)
		egen pmin = rmax(min_high min_low)
		
		gen trimmed = (int_res<=pmax & int_res>=pmin)
		drop max_high min_high max_low min_low pmax pmin
		
		
		bys iza_id702: egen trimmed_temp = min(trimmed)
	
		drop trimmed 
		rename trimmed_temp trimmed
		
		tab trimmed
		
		
		
		foreach type in  $outc {
	 	
		qui reg `type'  log_intensity_alt  $cov county_pair_*  [pw=1/fweight_reg], cluster(region)
	    matrix b=e(b)'
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		
		gen sam = 1 if e(sample)==1
		
		bys iza_id702 sam: gen insample =(_n==1)
		count if sam==1 & insample==1
		matrix n =r(N)
		drop sam insample
		
		matrix d0_`type'=b[1..1,1..1],se,n
		
		qui reg `type' (c.log_intensity_alt)##($cov2 i.region_pair2) [pw=1/fweight_reg], cluster(region)
		margins, dydx(log_intensity_alt)	
		matrix r=r(table)
		matrix b=r[1,1]
		matrix se=r[2,1]
		
		
		gen sam = 1 if e(sample)==1
		
		bys iza_id702 sam: gen insample =(_n==1)
		count if sam==1 & insample==1
		matrix n =r(N)
		drop sam insample
		
		
		matrix d1_`type'=b[1..1,1..1],se,n
		
		qui reg `type' (c.log_intensity_alt)##($cov2 i.region_pair2) if trimmed==1 [pw=1/fweight_reg], cluster(region)
		margins, dydx(log_intensity_alt)	
		matrix r=r(table)
		matrix b=r[1,1]
		matrix se=r[2,1]
		matrix p=r[4,1]
		
		gen sam = 1 if e(sample)==1
		
		bys pers_id sam: gen insample =(_n==1)
		count if sam==1 & insample==1
		matrix n =r(N)
		drop sam insample
		
		
		matrix d2_`type'=b[1..1,1..1],se,n
		
		
		matrix d_`type'=d0_`type',d1_`type',d2_`type'
		
	
}/*type*/
#delimit;
	matrix d=
	d_search_dist50 \
		d_log_distance_km \
		d_applications_total \
		d_applications_distant \
		d_applications_local \
		d_channel_active
	;
		
	
	matrix rownames d = 
		"Applied to distant" 
		"Search radius" 
		"#applications total" 
		"#applications distant" 
		"#applications local" 
		"Active search channel"
	;
	
	#delimit cr

	matrix colname d = "Baseline" "SE" "Obs" "AME" "SE" "Obs" "AME trimmed" "SE" "Obs" 
	estout matrix(d, fmt(4)) using $data/Table_A11_survey.tex, style(tex) replace
	
	
	
log close	

